package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.CommodityActivity;
import com.ailiwu.www.ailiwu.base.MyApplication;
import com.ailiwu.www.ailiwu.bean.SearchGiftBean;
import com.ailiwu.www.ailiwu.url.Urls;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/6 000622:54
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/6 0006       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class SearchGiftAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private LayoutInflater layoutInflater;
    private List<SearchGiftBean.DataEntity.ItemsEntity> data;
    private StringBuilder sb = new StringBuilder();
    private DisplayImageOptions options;

    public SearchGiftAdapter(Context context, List<SearchGiftBean.DataEntity.ItemsEntity> data) {
        this.context = context;
        this.data = data;
        layoutInflater = LayoutInflater.from(context);
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
         options = new DisplayImageOptions.Builder()
                .bitmapConfig(Bitmap.Config.RGB_565)                //设置图片的解码类型
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)   //设置图片以如何的编码方式显示
                .cacheInMemory(true)                                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                                  // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(10))          // 设置成圆角图片
                .build();                                           // 创建配置过得DisplayImageOption对象

    }


    // 创建ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_recycler_commodity, parent, false);
        return new MyViewHolder(view);
    }


    //进行数据的动态设置
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ((MyViewHolder)holder).tv_name.setText(data.get(position).getName());
        ((MyViewHolder)holder).tv_price.setText(data.get(position).getPrice());
        ((MyViewHolder)holder).tv_favorites_count.setText(
                getFavoritesCount(data.get(position).getFavorites_count()));


        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(MyApplication.mContext));
        imageLoader.displayImage(data.get(position).getCover_image_url(), ((MyViewHolder) holder).cover_image, options);

        ((MyViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = data.get(position).getId();
                String url_commodity = Urls.URL_COMMODITY + "/" + id;
                Intent intent = new Intent(MyApplication.mContext, CommodityActivity.class);
                intent.putExtra("url_commodity", url_commodity);
                intent.putExtra("COMMODITYID", id);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView cover_image;
        private TextView tv_name, tv_price, tv_favorites_count;
        View view;

        public MyViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            itemView.setOnClickListener(this);           // 对每个itemView设置点击监听
            cover_image = (ImageView) itemView.findViewById(R.id.cover_image);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_favorites_count = (TextView) itemView.findViewById(R.id.tv_favorites_count);
        }

        @Override
        public void onClick(View v) {

        }
    }
    /**
     * 格式喜欢的数量00.0k
     *
     * @param favorites_count 喜欢数量
     * @return 格式化后的喜欢数量字符串
     */
    public String getFavoritesCount(int favorites_count) {
        sb.delete(0, sb.length());
        String thousand = favorites_count / 1000 + "";
        sb.append(thousand + ".");
        String hundred = favorites_count % 1000 / 100 + "";
        sb.append(hundred + "k");
        return sb.toString();
    }
}
